package com.example.work4.http

import android.content.Context
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.work4.adapter.ChatListAdapter
import com.example.work4.adapter.MessageListAdapter
import com.example.work4.common.FIN
import com.example.work4.http.body.po.ChatInfoPage
import com.example.work4.http.imp.MessageRetrofit
import com.example.work4.po.Message
import com.example.work4.po.ResultBean
import com.example.work4.po.SSOInfo
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class MessageRetrofitImp(var context:Context) {

    private val messageRetrofitImp = BaseRetrofit.get(FIN.BaseUrl).create(MessageRetrofit::class.java)

    fun messageList(recycler:RecyclerView,adapter:ChatListAdapter) {
        SSOInfo.token?.let { messageRetrofitImp.messageList(it) }
            ?.enqueue(object : Callback<ResultBean<List<ChatInfoPage>>> {
                override fun onResponse(call: Call<ResultBean<List<ChatInfoPage>>>, response: Response<ResultBean<List<ChatInfoPage>>>) {
                    if (response.code() == 200) {
                        adapter.data = response.body()!!.data!!
                        recycler.adapter = adapter
                    }
                }
                override fun onFailure(call: Call<ResultBean<List<ChatInfoPage>>>, t: Throwable) {
                    Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                }
            })
    }

    fun get(recycler:RecyclerView,adapter:MessageListAdapter,id:Int) {
        SSOInfo.token?.let { messageRetrofitImp.get(it,id) }
            ?.enqueue(object : Callback<ResultBean<List<Message>>> {
                override fun onResponse(call: Call<ResultBean<List<Message>>>, response: Response<ResultBean<List<Message>>>) {
                    if (response.code() == 200) {
                        adapter.data = (response.body()!!.data as ArrayList<Message>?)!!
                        recycler.adapter = adapter
                    }
                }
                override fun onFailure(call: Call<ResultBean<List<Message>>>, t: Throwable) {
                    Toast.makeText(context, "加载失败", Toast.LENGTH_SHORT).show();
                }
            })
    }

    fun send(recycler: RecyclerView,adapter: MessageListAdapter, body: Message) {
        SSOInfo.token?.let { messageRetrofitImp.send(it,body) }
            ?.enqueue(object : Callback<ResultBean<Int>> {
                override fun onResponse(call: Call<ResultBean<Int>>, response: Response<ResultBean<Int>>) {
                    if (response.code() == 200) {
                        Toast.makeText(context, "发送成功！", Toast.LENGTH_SHORT).show()
                        adapter.data.add(body)
                        adapter.update()
                        recycler.scrollToPosition(adapter.itemCount -1);
                    }
                }
                override fun onFailure(call: Call<ResultBean<Int>>, t: Throwable) {
                    Toast.makeText(context, "发送失敗", Toast.LENGTH_SHORT).show();
                }
            })
    }

}